package com.atguigu.flink.day03;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

/**
 * @author Felix
 * @date 2023/12/2
 * 该案例演示了源算子--自定义source
 * 一般实现的是SourceFunction接口，重写其中的方法
 * env.addSource添加数据源
 */
public class Flink06_source_self {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env
            = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.addSource(new MySelfSource()).print();
        env.execute();
    }
}

class MySelfSource implements SourceFunction<String>{

    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        for (int i = 0; i < 1000000; i++) {
             ctx.collect("数据:" + i);
             Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        //当应用被取消后，执行cancel方法
        System.out.println("~~~结束~~~");
    }
}